package com.gph.etrip.model;


import java.util.ArrayList;

import com.gph.etrip.R;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewAdapter extends BaseAdapter {
    
    private Activity activity;
    private String[] data;
    private String url;
    //private ArrayList markername;
	private ArrayList<Marker> markers;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
	/**
	 * Constructor that instantiates the View Adapter
	 * 
	 * @param a The parent activity
	 * @param markers An ArrayList of the markers that is to be displayed in the Activity
	 */
    public ViewAdapter(Activity a, ArrayList<Marker> markers) {
    	activity = a;
    	this.markers = markers;
    	inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	imageLoader=new ImageLoader(activity.getApplicationContext());
    }
    
//    public ViewAdapter(Activity a, String[] d, ArrayList<Marker> markers) {
//    	activity = a;
//    	data= d;
//    	this.markers = markers;
//    	inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    	imageLoader=new ImageLoader(activity.getApplicationContext());
//    }
    /**
     * Get the number of items in the list.
     * @return The number of items in the list.
     */
    public int getCount() {
        return markers.size();
    }
    /**
     * @deprecated Not used.
     */
    public Object getItem(int position) {
        return position;
    }
   /**
    * @deprecated Not used.
    */
   public long getItemId(int position) {
        return markers.get(position).getId();
   }
   /**
    * Get the name of the marker at a given location.
    * @param position The index in the document.
    * @return
    */
    public String getMarkerName(int position){
    	return markers.get(position).getLabel();
    }
    /**
     * 
     * @param position
     * @return
     */
    public String getMarkerIcon(int position){
    	System.out.println("done"+ markers.get(position).getIcon());
    	return markers.get(position).getIcon();	
    }
    /**
     * Container that manages the combination of an image, and a text
     * @author Hui Zhou
     *
     */
    public static class ViewHolder {
        public TextView text;
        public ImageView image;
    }
    /**
     * Provides an updated View for a given location in a list.
     * @param position The current position in the list.
     * @param convert View The view that is to be updated with new text and image.
     * @param parent 
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        String markerName;
        String iconLocation;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        } else  {
            holder=(ViewHolder)vi.getTag();
        }
        markerName = getMarkerName(position);
        iconLocation = getMarkerIcon(position);
        holder.text.setText(markerName);
        holder.image.setTag(getMarkerIcon(position));
        //imageLoader.DisplayImage(data[position], activity, holder.image);
        imageLoader.DisplayImage(iconLocation, activity, holder.image);
        
        return vi;
    }
}